package nccloud.framework.web.action.excutor;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import nc.vo.pub.BusinessException;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IHttpOperator;
import pers.bc.utils.file.FileUtilbc;
import pers.bc.utils.pub.JsonUtil;
import pers.bc.utils.pub.JudgeAssertUtil;
import pers.bc.utils.pub.LoggerUtil;
import pers.bc.utils.pub.PropertiesUtil;
import pers.bc.utils.pub.PubEnvUtil;
import pers.bc.utils.pub.StringUtil;

public class CommonActionExcutor implements IActionExcutor
{
    public Object excute(Object instance, IHttpOperator operator)
    {
        ICommonAction action = (ICommonAction) instance;
        
        // if (PubEnvUtil.equals("develop", System.getProperty("nc.runMode")))
        // {
        // LoggerUtil loggerUtil = LoggerUtil.getInstance("actionLogs");
        // loggerUtil.info(action.getClass().getName(), JsonUtil.compressJson(operator.getRequest().read()));
        // }
        String actionInfo = "";
        String actionThrow = "";
        String actions = "";
        try
        {
            String patch = FileUtilbc.getWorkServerPath() + File.separator + "resources" + File.separator + "server.properties";
            Map<String, String> pram = PropertiesUtil.getAllProperties(patch);
            actionInfo = StringUtil.valueOfEmpty(pram.get("actionInfo"));
            actionThrow = StringUtil.valueOfEmpty(pram.get("actionThrow"));
            actions = StringUtil.valueOfEmpty(pram.get("actions"));
        }
        catch (IOException e)
        {
            
        }
        
        try
        {
            if (PubEnvUtil.equals("true", actionInfo.toLowerCase()) || PubEnvUtil.equals("y", actionInfo.toLowerCase()))
            {
                if (!PubEnvUtil.containStr(action.getClass().getName(), actions.split(PubEnvUtil.COMMA)))
                    LoggerUtil.getInstance("actionlogs")
                        .info("当前活动action：" + action.getClass().getName() + "    "
                            + JsonUtil.compressJson(JsonUtil.toJSONString(operator.getRequest().read())) + " \r\n Json串：<"
                            + JsonUtil.compressJson(operator.getRequest().getJson() + ">"));
            }
        }
        catch (Exception e)
        {
            
        }
        Object object = null;
        if (PubEnvUtil.equals("true", actionThrow.toLowerCase()) || PubEnvUtil.equals("y", actionThrow.toLowerCase()))
        {
            try
            {
                object = action.doAction(operator.getRequest());
            }
            catch (Throwable e)
            {
                LoggerUtil.getInstance("actionlogs")
                    .info("当前活动action：" + action.getClass().getName() + "    "
                        + JsonUtil.compressJson(JsonUtil.toJSONString(operator.getRequest().read())) + " \r\n Json串：<"
                        + JsonUtil.compressJson(operator.getRequest().getJson() + ">"));
                // LoggerUtil.getInstance("actionlogs" + File.separator + action.getClass().getSimpleName())
                // .exception(action.getClass().getName(), e);
                JudgeAssertUtil.throwException(e, "actionlogs" + File.separator + action.getClass().getSimpleName(),
                    action.getClass().getName());
                
            }
        }
        else
            object = action.doAction(operator.getRequest());
        
        return object;
    }
}
